var app = getApp()
Page({
  data: {
    ableAddFriend: false,
    show: false,
    deadline1: '',
    deadline2: '',
    challenge: {
      _openid: '',
      title: '',
      tag: '0', //分类
      description: "",
      gender: 0,
      deadline: '',
      publish_time: "",
      time: '',
      formId: ""
    }
  },
  onLoad() {

  },
  onReady() {
    this.setData({
      show: true
    })
  },
  onUnload() {
    //console.log("sd")
  },
  DateChange(e) {
    this.setData({
      deadline1: e.detail.value
    })
  },
  TimeChange(e) {
    this.setData({
      deadline2: e.detail.value
    })
  },

  doSubmit(e) {
    var time = new Date()
    var theTime = time.getFullYear() + '年' + (time.getMonth() + 1) + "月" + time.getDate() + "日 "
    if (time.getHours() < 10) {
      theTime += '0'
    }
    theTime += time.getHours() + ":"
    if (time.getMinutes() < 10) {
      theTime += '0'
    }
    theTime += time.getMinutes()
    this.setData({
      'challenge._openid': app.globalData.openid,
      'challenge.formId': e.detail.formId,
      'challenge.publish_time': new Date().getTime(),
      'challenge.time': theTime,
      'challenge.deadline': this.data.deadline1 + ' ' + this.data.deadline2,
      'challenge.gender': app.globalData.userInfo.gender
    })
    if (this.data.challenge.title < 3) {
      qq.showToast({
        icon: "none",
        title: "标题不能少于三个字哦~"
      })
    }
    else if (this.data.challenge.description.length < 3) {
      qq.showToast({
        icon: "none",
        title: "描述信息不能小于三个字哦~"
      })
    } else if (this.data.deadline1 == '' || this.data.deadline2 == "") {
      qq.showToast({
        icon: 'none',
        title: '请选择截止日期和时间'
      })
    } else {
      this.addToDB()
    }
  },

  addToDB() {
    var data = JSON.stringify(this.data.challenge)
    qq.request({
      url: 'https://api.weixin.qq.com/tcb/databaseadd?access_token=' + app.globalData.access_token, // 仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json' // 默认值
      }, method: "post",
      data: {
        "env": app.globalData.myEnv,
        "query": "db.collection(\"challenge\").add({\
            data: "+ data + "\
        })"
      },
      success(res) {
        qq.hideLoading()
        qq.showToast({
          title: "上传成功"
        })
         qq.navigateBack()
        //console.log(res.data)
      }
    })
  },
  inputTitle(e) {
    this.setData({
      'challenge.title': e.detail.value
    })
  },
  input1(e) {
    this.setData({
      'challenge.description': e.detail.value
    })
    // //console.log(e)
  },

  tagChange(e) {
    this.setData({
      'challenge.tag': e.currentTarget.id
    })
    // //console.log(e)
  }
})